package cn.ibdsr.human.core.check;

import org.springframework.util.StringUtils;

import java.util.regex.Pattern;

/**
 * @description:
 * @author: cdl
 * @email: iamcimon@qq.com
 * @date: 2021/8/2 13:17
 */
public class CheckUtil extends StringUtils {
    public static final String EMAIL = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
    public static final String MOBILE_PHONE = "^1[3|4|5|6|7|8|9][0-9]\\d{8}$";
    public static final String FIX_PHONE = "^((\\d{3,4}([\\- ]?))?)\\d{3,8}((\\-\\d{1,6})?)$";
    public static final String NUMBER = "^[0-9]*[1-9][0-9]*$";
    public static final String FLOAT_NUMBER = "^\\d+(\\.\\d+)?$";
    public static final String ID_CARD = "^\\d{15}$|^\\d{17}[0-9Xx]$";
    public static final String URL = "^(?:((ht|f)tps?)://)?(?:(www.)?+[A-Za-z0-9\\.-]+\\.[A-Za-z]{2,3}|\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})(:\\d{1,5})?(?:/[a-zA-Z0-9-\\._\\?,'\\+&%\\$=~\\*!:@#\\\\]*)*$";
    public static final String ACCOUNT = "^[0-9a-zA-Z\\u4e00-\\u9fa5]{2,15}$";
    public static final String PASSWORD = "^(?![\\d]+$)(?![a-zA-Z]+$)(?![!#$%^&*]+$)[\\da-zA-Z!#$%^&*]{6,16}$";
    public static final String ROLENAME = "^[0-9a-zA-Z\\u4e00-\\u9fa5]{2,15}$";
    public static final String NAME = "(^[\\u4e00-\\u9fa5]{1}[\\u4e00-\\u9fa5\\.·。]{0,8}[\\u4e00-\\u9fa5]{1}$)|(^[a-zA-Z]{1}[a-zA-Z\\s*]{0,18}[a-zA-Z]{1}$)";
    public static final String webSiteRegexp = "(ht|f)tp(s?)\\:\\/\\/[0-9a-zA-Z]([-.\\w]*[0-9a-zA-Z])*(:(0-9)*)*(\\/?)([a-zA-Z0-9\\-\\.\\?\\,\\'\\/\\\\\\+&amp;%\\$#_]*)?";
    public static final String imageURLRegexp = "[http]+://[^\\s]*[\\.jpg|\\.png|\\.jpeg|\\.bmp|\\.gif]";
    public static final String LISCENSE_ID = "^([0-9a-zA-Z]{18}$|\\d{15}$)";

    public CheckUtil() {
    }

    public static boolean isLiscenseId(String licenseId) {
        return checkReg("^([0-9a-zA-Z]{18}$|\\d{15}$)", licenseId);
    }

    public static boolean isEmail(String email) {
        return checkReg("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$", email);
    }

    public static boolean isPhone(String phone) {
        return checkReg("^((\\d{3,4}([\\- ]?))?)\\d{3,8}((\\-\\d{1,6})?)$", phone);
    }

    public static boolean isMobile(String mobile) {
        return checkReg("^1[3|4|5|6|7|8|9][0-9]\\d{8}$", mobile);
    }

    public static boolean isNumber(String number) {
        return checkReg("^[0-9]*[1-9][0-9]*$", number);
    }

    public static boolean isFloat(String number) {
        return checkReg("^\\d+(\\.\\d+)?$", number);
    }

    public static boolean isUrl(String url) {
        return checkReg("^(?:((ht|f)tps?)://)?(?:(www.)?+[A-Za-z0-9\\.-]+\\.[A-Za-z]{2,3}|\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})(:\\d{1,5})?(?:/[a-zA-Z0-9-\\._\\?,'\\+&%\\$=~\\*!:@#\\\\]*)*$", url);
    }

    public static boolean isIDCard(String IDCard) {
        return checkReg("^\\d{15}$|^\\d{17}[0-9Xx]$", IDCard);
    }

    public static boolean isAccount(String account) {
        return checkReg("^[0-9a-zA-Z\\u4e00-\\u9fa5]{2,15}$", account);
    }

    public static boolean isPassword(String password) {
        return checkReg("^(?![\\d]+$)(?![a-zA-Z]+$)(?![!#$%^&*]+$)[\\da-zA-Z!#$%^&*]{6,16}$", password);
    }

    public static boolean checkReg(String reg, CharSequence obj) {
        return Pattern.compile(reg).matcher(obj).matches();
    }
}
